package cn.doitedu.rtmk.demo2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.runtime.state.hashmap.HashMapStateBackend;
import org.apache.flink.streaming.api.CheckpointingMode;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author: deep as the sea
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2023/4/14
 * @Desc: 学大数据，到多易教育
 * <p>
 * Demo2相比demo1来说，规则变得更复杂一点： 多了静态画像条件
 * 比如规则如下：
 * 静态圈选条件：月平均消费额>200的用户
 * 触发条件：当他发生A行为时（event_id='page_load', properties['url'] = '/page/001'），命中规则
 **/
public class Demo2 {
    public static void main(String[] args) throws Exception {

        // 创建编程环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(2000, CheckpointingMode.EXACTLY_ONCE);
        env.getCheckpointConfig().setCheckpointStorage("file:/d:/ckpt");
        env.getCheckpointConfig().setTolerableCheckpointFailureNumber(3);
        env.setParallelism(1);
        env.setStateBackend(new HashMapStateBackend());


        // 从kafka中读取用户的实时行为
        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("doitedu:9092")
                .setStartingOffsets(OffsetsInitializer.latest())
                .setTopics("dwd-events-detail")
                .setClientIdPrefix("rtmk-")
                .setGroupId("g-rtmk-")
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        DataStreamSource<String> ds = env.fromSource(source, WatermarkStrategy.noWatermarks(), "s");
        SingleOutputStreamOperator<UserEvent> eventStream = ds.map(json -> JSON.parseObject(json, UserEvent.class));

        SingleOutputStreamOperator<JSONObject> resultStream =
                eventStream.keyBy(UserEvent::getUser_id)
                        .process(new RuleEngineCoreFunction2());

        resultStream.print();
        env.execute();
    }
}
